package arithmetic;

import java.io.*;

public class NoteWriter {
    public static String destdir = "C:\\Users\\86158\\Desktop\\chapter06";//目标文件夹位置
    public static String cppplace = "C:\\Users\\86158\\Desktop\\chapter";//包含cpp文件的文件夹
    public static String note = "/*\n" +
            " *********************************************************************************************************\n" +
            " *\n" +
            " * 功能：   作业E1_1\n" +
            " *\n" +
            " * 文件名： E1_1.cpp\n" +
            " *\n" +
            " * 作者：     刘森森\n" +
            " *\n" +
            " * 单位：   河南大学计算机与信息工程学院\n" +
            " *\n" +
            " * E-mail:  1926105235@qq.com\n" +
            " *\n" +
            " * 时间：   2021月5月1日\n" +
            " *\n" +
            " * 版本：   1.0\n" +
            " *\n" +
            " *********************************************************************************************************\n" +
            " */";

    public static void writenote(File f, String dest) {

        FileInputStream fis = null;
        FileOutputStream fout = null;
        InputStreamReader is = null;
        OutputStreamWriter osw = null;
        try {
            fis = new FileInputStream(f);
            fout = new FileOutputStream(dest);
            is = new InputStreamReader(fis, "GBK");
            osw = new OutputStreamWriter(fout, "GBK");
            BufferedReader br = new BufferedReader(is);
            String tmp = "";
            String content = "";
            try {
                while ((tmp = br.readLine()) != null) {
                    osw.write(tmp + "\r\n");
                    if (tmp.endsWith("namespace std;")) {
                        content = note.replace("功能：   作业E2_1", "功能：   作业" + f.getName().replace(".cpp", ""));
                        content = content.replace("功能：   作业E2_1", "功能：   作业" + f.getName().replace(".CPP", ""));
                        content = content.replace("文件名： E2_1.cpp", "文件名： " + f.getName());
                        content = content.replace("文件名： E2_1.cpp", "文件名： " + f.getName());
                        osw.write(content + "\r\n");
                    }

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } finally {

            if (osw != null) {
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        if (fis != null) {
            try {
                fis.close();


            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        if (fout != null) {
            try {
                fout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {

        File ddi = new File(destdir);
        if (!ddi.exists()) {
            ddi.mkdirs();
        }

        File directory = new File(cppplace);
        File[] files = directory.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                if (pathname.getName().contains(".cpp"))
                    return true;
                return false;
            }
        });
        for (File ff : files) {
            writenote(ff, destdir + "\\" + ff.getName());
        }

    }
}
